import type {RouteRecordRaw} from "vue-router";
import router, {layoutRouter, staticRouter} from "./index"
import {useRouterStore} from "@/stores/router";
import pinia from "@/stores";

const routerStore= useRouterStore(pinia);

const dynamicsRouter:RouteRecordRaw[] = [{
    name: 'dashboard',
    path: 'dashboard',
    component: () => import('@/views/dashboard/index.vue'),
    meta: {
        title: '工作台',
        component: "/src/views/dashboard/index.vue",
        id: 1,
        icon: "DataAnalysis"
    }
},{
    name: 'test',
    path: 'test',
    component: () => import('@/views/test/index.vue'),
    meta: {
        title: '测试页面',
        component: "/src/views/test/index.vue",
        id: 3,
        icon: 'SetUp'
    }
},{
    name: 'route_admin',
    path: 'route',
    meta: {
        title: '路由管理',
        id: 2,
        icon: 'Guide'
    },
    children: [{
        name: 'dynamicsRoute',
        path: '/dynamicsRoute',
        component: () => import("@/views/route/index.vue"),
        meta: {
            title: '动态路由',
            component: "/src/views/route/index.vue",
            id: 3,
            icon: 'Van'
        }
    }]
}]

export const getRouter = () => {
    return dynamicsRouter
}

export const loadRouter = async () => {
    //缓存路由
    routerStore.setRouterList(dynamicsRouter);
    layoutRouter.children = [...dynamicsRouter]
    //组合静态路由
    const routerList = [layoutRouter, ...staticRouter]

    routerList.forEach(route => {
        router.addRoute(route)
    })
}